package com.changou.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.system.entity.Result;
import com.changgou.system.goods.feign.CategoryFeign;
import com.changgou.system.goods.feign.SkuFeign;
import com.changgou.system.goods.feign.SpuFeign;
import com.changgou.system.goods.pojo.Category;
import com.changgou.system.goods.pojo.Sku;
import com.changgou.system.goods.pojo.Spu;
import com.changou.page.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {
    @Value("pagepath")
    String pagepath;
    @Autowired
    TemplateEngine templateEngine;//模板引擎
 @Autowired
    private SpuFeign spuFeign;
   @Autowired
 private SkuFeign skuFeign;
@Autowired
   private CategoryFeign categoryFeign;
    @Override
    public void generateItemPage(String spuId) {
        //获取context对象，存储数据
        Context context=new Context();
        Map<String,Object> itemData=this.getItemData(spuId);
        context.setVariables(itemData);
        Writer writer =null;
        try {
            //页面文件夹 存不存在
            File dir = new File(pagepath);
            if(!dir.exists()){
                dir.mkdir();
            }
            File  pageFile = new File(dir + "/" + spuId + ".html");
             writer=new PrintWriter(pageFile);
            //1模板名称 template
            //2 context 数据
            //writer
            templateEngine.process("item",context,writer);

        }catch (Exception e){

        } finally {
            //5关闭流

            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }



    }
    private  Map<String,Object> getItemData(String spuId){
        Map<String,Object> resultMap =new HashMap<>();
     //spu
        Spu spu = spuFeign.findSpuById(spuId).getData();
           resultMap.put("spu",spu);
   //sku
        List<Sku> skuListBySpuId =  skuFeign.findSkuListBySpuId(spuId);
        resultMap.put("skuList",skuListBySpuId);

     //category
        //一级分类
        Category category1 = categoryFeign.findById(spu.getCategory1Id()).getData();
      resultMap.put("category1",category1);
        //2级分类
        Category category2 = categoryFeign.findById(spu.getCategory2Id()).getData();
        resultMap.put("category2",category2);
       //3级分类
        Category category3 = categoryFeign.findById(spu.getCategory3Id()).getData();
        resultMap.put("category3",category3);
        //imageList
        resultMap.put("imageList",spu.getImages().split(","));

     // 5获取商品规格信息
        resultMap.put("specificationList", JSON.parseObject(spu.getSpecItems(),Map.class));

        return resultMap;
    }


}
